<!--
  - Copyright (c) 2018.
  - 北京九思金信科技有限公司对该文件内容保留所有权利。
  - 未经许可，不得私自拷贝、传播、发布、引用该文件内容，违者将被追究法律责任。
  -->

<template>
    <div>
        <span v-if="!editing">{{customer.customerName}}</span>
        <el-select  v-if="editing" v-model="customer"
                    placeholder="请选择客户"
                    value-key="customerId"
                    :remote-method="remoteMethod"
                    filterable
                    remote
                    reserve-keyword
                    @change="change"
                    :disabled="disabled">
            <el-option
                    v-for="item in customerData"
                    :key="item.customerId"
                    :label="item.customerName"
                    :value="item">
            </el-option>
        </el-select>
    </div>
</template>

<script>
export default {
    name: "FtRatingCustomer",
    props: {
        editing: {
            type: Boolean,
            default: true
        },
        disabled: {
            type: Boolean,
            default: false
        },
        value: [String, Object]
    },
    mounted(){
        this.loadAll()
    },
    data(){
        return{
            //客户主键
            customerId: '',
            //客户对象
            customer:{},
            //客户集合
            customerData:[]
        }
    },
    methods:{
        remoteMethod(query) {
            if (query) {
                let params = {
                    queryParam: query
                }
                this.$http.post('/joys-rest/lss-customer/rating/ratingCustomer', params).then(
                    resp => {
                        this.customerData = resp.body
                    }, resp => {
                        this.$message.error(resp.body)
                    }
                )
            } else {
                this.loadAll()
            }
        },
        //全部加载客户
        loadAll() {
            this.$http.post('/joys-rest/lss-customer/rating/ratingCustomer', {}).then(
                resp => {
                    this.customerData = resp.body
                }, resp => {
                    this.$message.error(resp.body)
                }
            )
        },
        //change事件
        change() {
            let customer = {}
            customer['customerId']=this.customer.customerId
            customer['customerCode']=this.customer.customerCode
            customer['customerName']=this.customer.customerName
            customer['identityType']=this.customer.identityType
            customer['identityNo']=this.customer.identityNo
            customer['industry']=this.customer.industry
            customer['industryBig']=this.customer.industryBig
            customer['enterpriseTypeId']=this.customer.enterpriseTypeId
            this.$emit('input', customer)
            this.$emit('change', customer)
        },
        //单个加载客户
        loadById() {
            this.customer = {}
            if (this.customerId) {
                var url = '/joys-rest/lss-customer/rating/ratingCustomer/'
                this.$http.get(url + this.customerId).then(
                    resp => {
                        this.customer = resp.body
                    }, resp => {
                        this.$message.error(resp.body.message)
                    }
                )
            }
        },
    },
    watch: {
        value(val) {
            this.customerId = val
            this.loadById()
        },
    }
}
</script>

<style scoped>

</style>
